/*
 * Copyright 2009 Xoomo Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.xoomo.commons.exception;


/**
 * @author f.gonzalez
 * @see org.hibernate.exception.ExceptionUtils
 * */
public class ExceptionUtils {
	
	/**
	 * @return true si alguna causa contiene el string indicado en su mensaje
	 * @param exception excepcion 
	 * @param string string buscado
	 * */
	public static boolean causesContains(Exception e, String string) {
		Throwable t = e;
		do {
			t = t.getCause();
			if (t == null)
				break;
			if (t.getMessage() != null && t.getMessage().contains(string)) {
				return true;
			}
		} while (t.getCause() != null);

		return false;
	}

	/**
	 * @return true si alguna causa es de la misma clase que la indicada
	 * @param exception excepcion 
	 * @param cause causa buscada
	 * */
	public static boolean causesContains(RuntimeException e,
			Throwable cause) {
		Throwable t = e;
		do {
			t = t.getCause();
			if (t == null)
				break;
			if (t.getClass().equals(cause.getClass())) {
				return true;
			}
		} while (t.getCause() != null);

		return false;
	}

}
